<?php
namespace App\Api\Controllers;

use App\Api\Models\CmcAfterSales;
use App\Api\Models\Member;
use App\Http\Controllers\Controller;
use App\Api\Services\JsonService;
use Illuminate\Http\Request;
use App\Api\Services\AliyunOss;

class CmcAfterSalesController extends Controller
{
    /**
     * 加载售后表单接口
     * @param $uid 用户id
     * @param $order_id 订单id
     * @return array
     */
    public function getAfterData(Request $request)
    {
        $validate = $request->validate([
            'uid'=>['required','alpha_num'],
            'order_id'=>['required','alpha_num'],
            'order_type'=>['required']
        ]);

        $phone = Member::getPhone($validate['uid']);

        if($phone){
            return JsonService::success([$validate['uid'],$validate['order_id'],$phone,$validate['order_type']]);
        }
        return JsonService::fail('请绑定手机号码',[$validate['uid'],$validate['order_id'],$validate['order_type']],'1003');
    }

    /**
     * 售后信息提交
     * @param Request $request
     * @return array
     */
    public function afterTalesPage(Request $request)
    {
        //使用validate验证
        $validate = $request->validate([
            'uid'=>['required','alpha_num'],
            'order_id'=>['required','alpha_num'],
            'reason'=>['required','alpha_num'],
            'type'=>['required','alpha_num'],
            'price'=>['required','alpha_num','max:50'],
            'explain'=>['required','alpha_dash','max:500'],
            'image'=>['required'],
            'phone'=>['required'],
            'order_type'=>['required']
        ]);

        $file = $request->file('image');

        $validate['image'] = config('aliyun.cui').AliyunOss::aliOss($file);

        $validate['status'] = 2;

        $allowFiled = [
            'uid',
            'order_id',
            'order_type',
            'status'
        ];

        $where = CmcAfterSales::getAfterTales($allowFiled,$validate['uid'],$validate['order_id'],$validate['order_type']);

        if($where){
            return JsonService::fail('已经提交过，请不要重复提交');
        }

        $casres = CmcAfterSales::insert($validate);

        if($casres){
            return JsonService::success('已经交由客服处理，请等待结果',$casres);
        }
        return JsonService::fail('申请失败','','1003');

    }

    /**
     * 售后成功页面信息接口
     * @param $order_id
     * @param $order_type
     * @return array
     */
    public function getGoodsSite(Request $request)
    {
        $validate = $request->validate([
            'order_id'=>['required','alpha_num'],
            'order_type'=>['required']
        ]);

        $siteData = CmcAfterSales::getSiteData($validate['order_id'],$validate['order_type']);

        if($siteData){
            return JsonService::success($siteData);
        }
        return JsonService::fail('暂无店铺信息','','1003');
    }

    /**
     * 撤销售后请求
     * @param Request $request
     */
    public function delSiteRevoke(Request $request)
    {
        $validate = $request->validate([
            'uid'=>['required','alpha_num'],
            'order_id'=>['required','alpha_num'],
            'order_type'=>['required']
        ]);

        $updGoodsSite = CmcAfterSales::updSiteData($validate['uid'],$validate['order_id'],$validate['order_type']);

        if($updGoodsSite == 0){
            return JsonService::fail('撤销失败,可能已撤回或不存在','','1003');
        }
        return JsonService::success('已成功撤销');
    }

    /**
     * 订单号添加
     * @param Request $request
     */
    public function addCourierNumber(Request $request)
    {
        $validate = $request->validate([
            'uid'=>['required','alpha_num'],
            'order_id'=>['required','alpha_num'],
            'order_type'=>['required'],
            'courier_number'=>['required','alpha_num'],
            'return_message'=>['required'],
            'courier_price'=>['required']
        ]);

        $file = $request->file('courier_image');

        $validate['courier_image'] = config('aliyun.cui').AliyunOss::aliOss($file);

        $data = [
            'courier_number'=>$validate['courier_number'],
            'return_message'=>$validate['return_message'],
            'courier_price'=>$validate['courier_price'],
            'courier_image'=>$validate['courier_image']
        ];

        $addRes = CmcAfterSales::creSiteData($validate['uid'],$validate['order_id'],$data,$validate['order_type']);

        if($addRes == 0){
            return JsonService::fail('订单号添加失败','','1003');
        }
        return JsonService::success('订单号添加成功');
    }
}

